﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Web.Library.DomainObject.SQLServer;
using System.Data;
using Dapper;

namespace Web.Library.DataAccess.SQLServer
{
    public partial class SysAppGroupAuthorizationRepo
    {
        public bool UpdateInsert(SysAppGroupAuthorization domain)
        {
            int result = 0;
            using (IDbConnection conn = base.GetConnection(connectionString))
            {
                //string sqlQuery = "UPDATE dbo.SysGroupAuthorization SET GroupID = @GroupID, MenuID = @MenuID, AllowDisplay = @AllowDisplay, AllowUpdate = @AllowUpdate, AllowDelete = @AllowDelete, CreteadBy = @CreteadBy, UpdatedDate = @UpdatedDate, UpdatedBy = @UpdatedBy WHERE AuthID = @AuthID";

                string sqlQuery = @"
DECLARE @Exist int
    
    SELECT @Exist = COUNT(AppAuthID)
    FROM SysAppGroupAuthorization
    WHERE GroupAppID = @GroupAppID AND AppID = @AppID
    
	IF @Exist > 0
	BEGIN
		UPDATE SysAppGroupAuthorization SET
			--Rowstatus = @Rowstatus,
			AllowDisplay = @AllowDisplay,
			--AllowCreate = @AllowCreate,
			--AllowUpdate = @AllowUpdate,
			--AllowDelete = @AllowDelete,
			UpdatedBy = @UpdatedBy,
			UpdatedDate = @UpdatedDate
		WHERE GroupAppID = @GroupAppID AND AppID = @AppID
		
		--SELECT 0 AS 'ID'
	END
	ELSE
	BEGIN
		INSERT INTO SysAppGroupAuthorization(
			--Rowstatus,
			GroupAppID,
			AppID,
			AllowDisplay,
			--AllowCreate,
			---AllowUpdate,
			--AllowDelete,
			CreatedBy,
			CreatedDate
		)
		VALUES(
			---0,
			@GroupAppID,
			@AppID,
			@AllowDisplay,
			--@AllowCreate,
			--@AllowUpdate,
			--@AllowDelete,
			@CreatedBy,
			@CreatedDate
		)
		
		--SELECT @@IDENTITY AS 'ID'
	END";

                try
                {
                    conn.Open();
                    result = conn.Execute(sqlQuery, domain);
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open) conn.Close();
                }
            }
            return (result > 0);
        }

        public SysAppGroupAuthorization RetrieveMenuAuthorizationByGroupIDAndMenuID(int GroupAppID, int AppID)
        {
            SysAppGroupAuthorization tt = new SysAppGroupAuthorization();
            using (IDbConnection conn = base.GetConnection(connectionString))
            {
                try
                {
                    conn.Open();
                    string strQuery = @"
                                        SELECT AppAuthID, GroupAppID, AppID, AllowDisplay, CreatedDate, CreatedBy, UpdatedDate, UpdatedBy FROM dbo.SysAppGroupAuthorization with(nolock) WHERE GroupAppID = @GroupAppID AND AppID = @AppID";

                    //tt = conn.Query<SysGroupAuthorization>(strQuery, new { MenuID = MenuID, GroupID = GroupID }).ToList<SysGroupAuthorization>();

                    tt = conn.Query<SysAppGroupAuthorization>(strQuery, new { GroupAppID = GroupAppID, AppID = AppID }).SingleOrDefault();
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open) conn.Close();
                }
            }
            return tt;
        }
    }
}
